<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    

<head>
	<script type="text/javascript">
		$(function(){
				init();
			});
		function init(){
			$('#t-privileges').datagrid({
				onBeforeEdit:function(index,row){
					row.editing = true;
					$(this).datagrid('refreshRow', index);
				},
				onAfterEdit:function(index,row){
					row.editing = false;
					row.isNewRecord = false;
					$(this).datagrid('refreshRow', index);
				},
				onCancelEdit:function(index,row){
					row.editing = false;
					row.isNewRecord = false;
					$(this).datagrid('refreshRow', index);
				}
			});
		}
		function formatActions(value, row, index){
			var s = '';
			if (row.editing){
				s += '<a href="#" onclick="saveRow(' + index + ')">保存</a>&nbsp;';
				s += '<a href="#" onclick="cancelRow(' + index + ')">取消</a>';
			} else {
				s += '<a href="#" onclick="editRow(' + index + ')">修改</a>&nbsp;';
				s += '<a href="#" onclick="destroyRow(' + index + ')">删除</a>';
			}
			return s;
		}
		
		function addRow(){
			var t = $('#t-privileges');
			t.datagrid('appendRow', {
				name:'',
				description:'',
				isNewRecord:true
			});
			var index = t.datagrid('getRows').length-1;
			editRow(index);
		}
		function editRow(index){
			var t = $('#t-privileges');
			t.datagrid('beginEdit', index);
		}
		function saveRow(index){
			var t = $('#t-privileges');
			var row = t.datagrid('getRows')[index];
			if (row.isNewRecord){
				var url = '<c:url value="/system/privilegeType/save"/>';
			} else {
				var url = '<c:url value="/system/privilegeType/update"/>?id=' + row.id;
			}
			var editors = t.datagrid('getEditors', index);
			var name = editors[0].target.val();
			var description = editors[1].target.val();
			$.ajax({
				url:url,
				type:'post',
				data:{
					name:name,
					description:description
				},
				dataType:'json',
				success:function(data){
					var data = eval(data);
					t.datagrid('endEdit', index);
					t.datagrid('getRows')[index] = data.row;
				}
			});
		}
		function cancelRow(index){
			var t = $('#t-privileges');
			var row = t.datagrid('getRows')[index];
			if (row.isNewRecord){
				t.datagrid('cancelEdit', index);
				t.datagrid('deleteRow', index);
			} else {
				t.datagrid('cancelEdit', index);
			}
		}
		function destroyRow(index){
			var id = $('#t-privileges').datagrid('getRows')[index].id;
			$.ajax({
				url:'<c:url value="/system/privilegeType/destroy"/>?id=' + id,
				type:'post',
				success:function(){
					$('#t-privileges').datagrid('deleteRow', index);
				}
			});
		}
		
		function move(dir){
			var row = $('#t-privileges').datagrid('getSelected');
			$.getJSON('<c:url value="/system/privilegeType/move"/>', {id:row.id,dir:dir}, function(){
				$('#t-privileges').datagrid('reload');				
			});
		}
	</script>
</head>
<body>
	<div class="easyui-layout" fit="true">
		<div region="north" border="false" style="height:40px;padding:5px;overflow:hidden;"  >
			<a href="<c:url value='/system/privilege/index'/>?mmid=${mm.id}" class="easyui-linkbutton" iconCls="icon-back" plain="true">返回</a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addRow()">新增</a>
			<a href="#" class="easyui-linkbutton" plain="true" onclick="move('up')">上移</a>
			<a href="#" class="easyui-linkbutton" plain="true" onclick="move('down')">下移</a>	
		</div>
		<div region="center" border="true">
			<table id="t-privileges" style="display: none;"
					url="<c:url value='/system/privilegeType/getAll'/>"
					border="false" singleSelect="true" fit="true"
					rownumbers="true">
				<thead>
					<tr>
						<th field="name" width="150" editor="text">分类名称</th>
						<th field="description" width="350" editor="text">分类描述</th>
						<th field="actions" width="80" align="center" formatter="formatActions">操作</th>
					</tr>
				</thead>
			</table>
		</div>
	</div>
</body>
